package cn.xorange.commons.exception;

import cn.xorange.commons.datasource.constants.DataErrorCode;
import cn.xorange.commons.utils.exception.ExceptionUtils;

/**
 * @author : yangjian
 * @date : 2023/8/23
 */
public class DataExceptionUtils {
    DataExceptionUtils(){}

    public static DataErrorCode runException(Throwable throwable){
        String causeMessage = throwable.getMessage();
        if (causeMessage.contains("SQLIntegrityConstraintViolationException")||causeMessage.contains("DuplicateKeyException")){
            return DataErrorCode.DUPLICATE_KEY_EXCEPTION;
        }else if(causeMessage.contains("BindingException")){
            return DataErrorCode.IBATIS_BINDING_ERROR;
        }else if(causeMessage.contains("Failed to obtain JDBC Connection")
                || causeMessage.contains("Communications link failure")){
            return DataErrorCode.DATABASE_LINK_FAILURE;
        }else if(causeMessage.contains("error in your SQL")|| causeMessage.contains("java.sql.SQLException")){
            return DataErrorCode.SQL_ERROR;
        }else {
            DataErrorCode code = DataErrorCode.DATABASE_RUN_EXCEPTION;
            code.setMsg(code.getMsg() + ": " + ExceptionUtils.getCauseMsg(throwable));
            return code;
        }

    }



}
